#ifndef SIMUBOARD_UTIL_HPP
#define SIMUBOARD_UTIL_HPP

#include <sstream>
#include <string>
#include <FL/Fl.H>
#include <Core/GPIO.h>

namespace GUI
{
    constexpr Fl_Color hex_to_fl_color(const std::string& hex_string)
    {
        if (hex_string.length() != 7 || hex_string[0] != '#')
        {
            return FL_BLACK;
        }

        const std::string r_hex = hex_string.substr(1, 2);
        const std::string g_hex = hex_string.substr(3, 2);
        const std::string b_hex = hex_string.substr(5, 2);

        unsigned int r, g, b;
        std::stringstream ss;

        ss << std::hex << r_hex;
        ss >> r;
        ss.clear();

        ss << std::hex << g_hex;
        ss >> g;
        ss.clear();

        ss << std::hex << b_hex;
        ss >> b;

        return fl_rgb_color(static_cast<uchar>(r), static_cast<uchar>(g), static_cast<uchar>(b));
    }

    inline std::pair<bool, bool> getGPIOState(const uint16_t pin)
    {
        if (pin < 0 || pin > 15) return {false, false};

        const uint16_t pin_mask = (1 << pin);
        bool is_output = (GPIO.DIR & pin_mask) ? true : false;
        bool is_high = (GPIO.ODR & pin_mask) ? true : false;
        return {is_output, is_high};
    }
}

#endif //SIMUBOARD_UTIL_HPP
